import request from "@/utils/request";

import type {
  getUserListResponse,
  SaveUserParams,
  GetUserRoleListResponse,
} from "./model/userModel";

enum Api {
  getUserListUrl = "/admin/acl/user",
  getSaveUserUrl = "/admin/acl/user/save",
  getDelUserUrl = "/admin/acl/user/remove",
  getUserRoleListUrl = "/admin/acl/user/toAssign",
  assignUserRoleListUrl = "/admin/acl/user/doAssign",
}

export const getUserListApi = (page: number, limit: number, username = "") => {
  return request.get<any, getUserListResponse>(
    Api.getUserListUrl + `/${page}/${limit}`,
    {
      params: {
        username,
      },
    }
  );
};

export const getSaveUserApi = (data: SaveUserParams) => {
  return request.post<any, null>(Api.getSaveUserUrl, data);
};

export const getDelUserApi = (id: string) => {
  return request.delete<any, null>(Api.getDelUserUrl + `/${id}`);
};

//获取角色数据
export const getUserRoleListApi = (userId: string) => {
  return request.get<any, GetUserRoleListResponse>(
    Api.getUserRoleListUrl + `/${userId}`
  );
};

//保存角色
export const assignUserRoleListApi = (userId: string, roleId: string[]) => {
  return request.post<any, null>(
    Api.assignUserRoleListUrl,
    {},
    {
      params: {
        userId,
        roleId: roleId.join(","),
      },
    }
  );
};
